package com.hk.hkvedio.config;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hk.hkvedio.model.Vedio;
import com.hk.hkvedio.monitor.HkVideoMonitor;
import com.hk.hkvedio.service.IHkVideoService;
import com.hk.hkvedio.service.VedioService;
import com.hk.hkvedio.utils.HCNetSDK;
import com.sun.jna.NativeLong;
import com.sun.jna.Pointer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentMap;


/**
 * @author liu
 */
@Component
public class ApplicationRunnerImpl implements ApplicationRunner {


    @Autowired
    VedioService vedioService;


    @Autowired
    IHkVideoService hkVideoService;

    private HkVideoMonitor hkVideoMonitor;


    @Override
    public void run(ApplicationArguments args) throws Exception {


        //初始化配置
        HCNetSDK hkSdk = DataParam.getInstance().getHkSdk();
        ConcurrentMap<Integer, NativeLong> userMap = DataParam.getInstance().getUserMap();
        if (Objects.equals(hkSdk, null)) {
            hkSdk = hkVideoService.net_dvr_init();
        }

        //初始化注册登录
        List<Vedio> list = vedioService.list(new LambdaQueryWrapper<Vedio>().eq(Vedio::getIsEnable, true));

        for (Vedio a : list) {
            NativeLong nativeLong = hkVideoService.net_dvr_logins_v40(a.getIp(), a.getLoginName(), a.getPassWord(), Short.parseShort(a.getMPort()));
            userMap.put(a.getId(), nativeLong);
        }

        bk(hkSdk);

        System.out.println("摄像头监听初始化完成！");
    }


    /**
     * 布控
     */
    private void bk(HCNetSDK hkSdk) {
        System.out.println("开始启动监听启动");
        hkVideoMonitor = new HkVideoMonitor();
        Pointer pUser = null;
        //设置警报信息回调
        boolean b = hkSdk.NET_DVR_SetDVRMessageCallBack_V30(hkVideoMonitor, pUser);
        System.out.println("错误码：" + hkSdk.NET_DVR_GetLastError());
        if (!b) {
            System.out.println("设置回调函数失败!");
        } else {
            System.out.println("设置回调函数成功!");
        }

        ConcurrentMap<Integer, NativeLong> userMap = DataParam.getInstance().getUserMap();
        for (Map.Entry<Integer, NativeLong> e : userMap.entrySet()) {
            NativeLong alarmChan = hkSdk.NET_DVR_SetupAlarmChan_V30(e.getValue());
            if (alarmChan.longValue() == -1) {
                System.out.println("设备ID" + e.getKey() + "错误码：" + hkSdk.NET_DVR_GetLastError());
                System.out.println("设备ID+" + e.getKey() + "+布防失败");
            } else {
                System.out.println("设备ID" + e.getKey() + "布防成功！");
            }
        }

        //本地IP地址置为null时自动获取本地IP
        NativeLong lListenHandle = hkSdk.NET_DVR_StartListen_V30(null, (short) 9211, hkVideoMonitor, null);
        if (lListenHandle.intValue() == -1) {
            System.out.println("错误码：" + hkSdk.NET_DVR_GetLastError());
            System.out.println("开始监听失败");
        } else {
            System.out.println("开始监听。。。。");
        }
    }
}
